package com.sf.osms.dep.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.springframework.stereotype.Repository;

import com.sf.novatar.base.dao.BaseBeanDao;
import com.sf.osms.dep.dao.IAgentConfigDao;
import com.sf.osms.dep.domain.AgentConfig;

@Repository
public class IAgentConfigDaoImpl extends BaseBeanDao<Long,AgentConfig> implements IAgentConfigDao{

	@SuppressWarnings("unchecked")
	@Override
	public List<AgentConfig> queryAgentConfigByAgentId(Long id) {
		String sql = "from  AgentConfig ac where ac.agentId = ?";
		Query query = this.getCurrentSession().createQuery(sql);
		query.setLong(0, id);		    
	    return query.list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public String querySFCode(String agentId, String agentCode) {
		String sql = "select sf_code from wbs_tt_agent_config  where agent_id = :agentId and agent_code = :agentCode";
		SQLQuery query = getCurrentSession().createSQLQuery(sql);
	    query.setString("agentId", agentId);
	    query.setString("agentCode", agentCode);
	    List<Object> list = query.list();
	    if(list == null){
	    	return null;
	    }
	    if(list.size() == 0){
	    	return null;
	    }
		return list.get(0).toString();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<String> querySFCode(String agentName) {
		String sql = "select b.sf_code from wbs_tt_agent a,wbs_tt_agent_config b where "+
				"a.id = b.agent_id and a.name = :agentName";
		SQLQuery query = getCurrentSession().createSQLQuery(sql);
	    query.setString("agentName", agentName);
	    List<String> list = query.list();
		return list;
	}

	@Override
	public List<String> queryAgentCodeByAgentId(Long agentId) {
		String sql = "select agent_code from wbs_tt_agent_config where "+
				"agent_id = :agentId";
		SQLQuery query = getCurrentSession().createSQLQuery(sql);
	    query.setLong("agentId", agentId);
	    List<String> list = query.list();
		return list;
	}

}
